package com.zhuxulin.tab;

import java.io.IOException;

import com.zhuxulin.tab.bluetooth.ISendMsg;
import com.zhuxulin.tab.datas.CloseChannelEventData;
import com.zhuxulin.tab.threads.SocketConnect;
import com.zhuxulin.tab.utils.LogUtils;

import android.os.Bundle;
import android.app.Activity;
import android.bluetooth.BluetoothSocket;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Setting extends Activity {
	protected static final String TAG = Setting.class.getSimpleName();
	private Button conn = null;
	private Button disConn = null;
	private Button clear = null;
	private App app = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		app = (App) this.getApplication();
		conn = (Button) findViewById(R.id.conn);
		disConn = (Button) findViewById(R.id.disconn);
		clear = (Button) findViewById(R.id.clear);
	}

	@Override
	public void onStart() {
		super.onStart();
		conn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LogUtils.i(TAG, "Conn...");
				new SocketConnect((App) getApplication(), Setting.this).start();
			}
		});
		disConn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				BluetoothSocket socket = null;
				socket = app.getSocket();
				try {
					if (socket != null) {
						if (socket.isConnected()) {
							socket.close();
							LogUtils.i(TAG, "Closed socket!");
						} else {
							LogUtils.i(TAG,
									"Socket is not connected do not need to close!");
						}
					} else {
						LogUtils.i(TAG, "Socket is null!");
					}
				} catch (IOException e) {
					LogUtils.i(TAG, "Cosle socket throw execption!");
					e.printStackTrace();
				}
			}
		});
		clear.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ISendMsg sendMsg = app.getSendMsg();
				if (sendMsg != null) {
					sendMsg.sendMessage(CloseChannelEventData.closeALL());
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.setting, menu);
		return true;
	}

}
